﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DM4;
using sscore;

namespace DM4Client.AddEditForms
{
    public partial class FrmAddEditMailAccount : DevExpress.XtraEditors.XtraForm
    {
        public int MailAccountId = DM4Consts.DefaultNULLId;
        public OperationResult Result = new OperationResult();
        public const string FrmCaption = "New mail account adding";

        public FrmAddEditMailAccount(int mailAccountId = DM4Consts.DefaultNULLId)
        {
            MailAccountId = mailAccountId;
            InitializeComponent();
        }

        private void FrmAddEditMailAccountShown(object sender, EventArgs e)
        {
            if (MailAccountId == DM4Consts.DefaultNULLId)
            {
                Text = FrmCaption;
            }
            else
            {
                DataRow[] drcoll = FrmMain.DtMailAccounts.Select(String.Format("[{0}]={1}", CnMailAccounts.Id, MailAccountId));
                if (drcoll.Length > 0)
                {
                    DataRow dr = drcoll[0];
                    Text = String.Format("Edit mail account: {0}", dr[CnMailAccounts.Mail]);
                    tbMail.Text = dr[CnMailAccounts.Mail].ToString();

                    tbSendServer.Text = dr[CnMailAccounts.SendServer].ToString();
                    tbSendPort.Text = dr[CnMailAccounts.SendPort].ToString();
                    tbSendUser.Text = dr[CnMailAccounts.SendUser].ToString();
                    tbSendPassword.Text = dr[CnMailAccounts.SendPassword].ToString();
                    chbSendUseSSL.Checked = (dr[CnMailAccounts.SendUseSsl].ToString() == DM4Consts.TRUE);

                    tbReceiveServer.Text = dr[CnMailAccounts.ReceiveServer].ToString();
                    tbReceivePort.Text = dr[CnMailAccounts.ReceivePort].ToString();
                    tbReceiveUser.Text = dr[CnMailAccounts.ReceiveUser].ToString();
                    tbReceivePassword.Text = dr[CnMailAccounts.ReceivePassword].ToString();
                    chbReceiveUseSSL.Checked = (dr[CnMailAccounts.ReceiveUseSsl].ToString() == DM4Consts.TRUE);
                }
                else
                {
                    Text = FrmCaption;
                }
            }

            tbMail.Focus();
        }

        private void btnOkClick(object sender, EventArgs e)
        {
            try
            {

                if (String.IsNullOrEmpty(tbMail.Text))
                {
                    MessageBox.Show(this, "Mail field is empty", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbMail.Focus();
                    return;
                }

                if (MailAccountId == DM4Consts.DefaultNULLId)
                {
                    Result = FrmMain.AddMailAccount(
                        tbMail.Text.Trim()
                        , tbSendServer.Text.Trim(), tbSendPort.Text.Trim()
                        , tbSendUser.Text.Trim(), tbSendPassword.Text.Trim()
                        , chbSendUseSSL.Checked
                        , tbReceiveServer.Text.Trim(), tbReceivePort.Text.Trim()
                        , tbReceiveUser.Text.Trim(), tbReceivePassword.Text.Trim()
                        , chbReceiveUseSSL.Checked);

                }
                else
                {
                    Result = FrmMain.EditMailAccount(MailAccountId
                        , tbMail.Text.Trim()
                        , tbSendServer.Text.Trim(), tbSendPort.Text.Trim()
                        , tbSendUser.Text.Trim(), tbSendPassword.Text.Trim()
                        , chbSendUseSSL.Checked
                        , tbReceiveServer.Text.Trim(), tbReceivePort.Text.Trim()
                        , tbReceiveUser.Text.Trim(), tbReceivePassword.Text.Trim()
                        , chbReceiveUseSSL.Checked);
                }

                if (Result.Result > 0)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(this, Result.Message, DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                string m = MailAccountId == DM4Consts.DefaultNULLId
                    ? "Error of mail account creating"
                    : "Error of mail account editing";
                FrmMain.WriteError(m + Environment.NewLine + ex.Message);
                Close();
            }
        }
    }
}